/** 重试机制 */
export class AxiosRetry {
  /** 重试 */
  retry(axiosInstance, error) {
    const { config } = error.response;
    const { waitTime, count } = config.requestOptions.retryRequest || {};

    config.__retryCount = config.__retryCount || 0;
    if (config.__retryCount >= count) {
      return Promise.reject(error);
    }
    config.__retryCount += 1;

    delete config.headers;

    // 获取新的 axios实例 ----- 直接用旧config进行请求
    return this.delay(waitTime).then(() => axiosInstance(config));
  }

  /** 延迟 */
  delay(waitTime) {
    return new Promise((resolve) => setTimeout(resolve, waitTime));
  }
}
